/* eslint no-constant-condition: ["error", { "checkLoops": false }] */
/**
 *
 * Copyright 2016-present scrm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
import { put, take, call, fork } from 'redux-saga/effects';

import * as types from '../constants/ActionTypes';
import ToastUtil from '../utils/ToastUtil';
import RequestXhrUtil from '../utils/RequestXhrUtil';
import { UA } from '../utils/AttachParamsUtil';
import { WEXIN_TEAM_LIST, SCRM_API } from '../constants/Urls';
import { fetchTeamList, receiveTeamList, startSaveTeam, responseSaveTeam, fetchTeamDetail, receiveTeamDetail } from '../actions/team';

export function* requestTeamList(
  token,
  site_id,
  isRefreshing,
  loading,
  isLoadMore,
  page
) {
  try {
    yield put(fetchTeamList(isRefreshing, loading, isLoadMore));
    const teamList = yield call(
      RequestXhrUtil.request,
      `${SCRM_API}/users/${token}/teams?site_id=${site_id}&page=${page}`,
      'get'
    );
    yield put(receiveTeamList(
      teamList.results.teams,
      false
    ));
    const errorMessage = teamList.errmsg;
    if (teamList.errcode && errorMessage !== '') {
      yield ToastUtil.showShort(errorMessage);
    }
  } catch (error) {
    yield put(receiveTeamList([], false));
    ToastUtil.showShort('网络发生错误，请重试');
  }
}

export function* requestTeamDetail(
  token,
  teamId
) {
  try {
    yield put(fetchTeamDetail());
    const teamDetailData = yield call(
      RequestXhrUtil.request,
      `${SCRM_API}/users/${token}/teams/${teamId}`,
      'get'
    );
    yield put(receiveTeamDetail(
      teamDetailData.results,
      false
    ));
    const errorMessage = teamDetailData.errmsg;
    if (teamDetailData.errcode && errorMessage !== '') {
      yield ToastUtil.showShort(errorMessage);
    }
  } catch (error) {
    yield put(receiveTeamDetail([], false));
    ToastUtil.showShort('网络发生错误，请重试');
  }
}

export function* requestSaveTeam(
  team,
  siteId,
  token
) {
  try {
    yield put(startSaveTeam(true));
    let url
    if (team.id) {
      url = `${SCRM_API}/users/${token}/teams/${team.id}`
    } else  {
      url = `${SCRM_API}/users/${token}/teams`
    }
    const response = yield call(
      RequestXhrUtil.request,
      url,
      'post',
      UA(team, {site_id: siteId})
    );
    console.log('EditTeam url', url)
    yield put(responseSaveTeam(
      team,
      response,
      false
    ));
    const errorMessage = response.errmsg;
    if (response.errcode && errorMessage !== '') {
      yield ToastUtil.showShort(errorMessage);
    }
  } catch (error) {
    console.log(error)
    yield put(responseSaveTeam([], [], false));
    ToastUtil.showShort('网络发生错误，请重试');
  }
}

export function* watchRequestTeamList() {
  while (true) {
    const {
      token, site_id, isRefreshing, loading, isLoadMore, page
    } = yield take(types.REQUEST_TEAM_LIST);
    yield fork(
      requestTeamList,
      token,
      site_id,
      isRefreshing,
      loading,
      isLoadMore,
      page
    );
  }
}

export function* watchRequestSaveTeam() {
  while (true) {
    const {
      team, siteId, token
    } = yield take(types.REQUEST_SAVE_TEAM);
    yield fork(
      requestSaveTeam,
      team,
      siteId,
      token
    );
  }
}

export function* watchRequestTeamDetail() {
  while (true) {
    const {
      token, teamId
    } = yield take(types.REQUEST_TEAM_DETAIL);
    yield fork(
      requestTeamDetail,
      token,
      teamId
    );
  }
}

